package users

import (
	"context"
	"server/chat_room_server/model/entity"

	"server/chat_room_server/internal/svc"
	"server/chat_room_server/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *LoginLogic) Login(req *types.LoginReq) (resp *types.LoginResp, err error) {
	// todo: add your logic here and delete this line
	name := req.UserName
	//password := req.Password
	db := l.svcCtx.DB
	var user entity.User
	tx := db.Debug().Where(" name = ? ", name).Find(&user)
	if tx.Error != nil {
		logx.Error(tx.Error)
		panic(tx.Error)
	}
	if tx.RowsAffected == 0 {
		logx.Infof("不存在用户：%v", name)
		return nil, nil
	}

	return &types.LoginResp{
		Id:   user.UserId,
		Name: user.Name,
	}, nil
}
